package com.msf.tutorial.java8.chapter1;

import com.msf.tutorial.java8.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 第一种状态：筛选绿苹果
 *
 * 缺点：
 * 不能灵活地应对需求的变更，
 * 如果此时又要选出红苹果，则需要再加一个 filterRedApples 方法
 *
 * 需求变更：需要筛选出红苹果
 */
public class Step1 {

    /**
     * 筛选绿苹果
     *
     * @param inventory
     * @return
     */
    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if ( "green".equals(apple.getColor())) {
                result.add(apple);
            }
        }
        return result;
    }

    public static void main(String[] args) {

        List<Apple> list = Arrays.asList(
            new Apple(1L, "red", 1D),
            new Apple(2L, "red", 2D),
            new Apple(3L, "green", null),
            new Apple(1L, "green", 3D)
        );

        List<Apple> greenApples = filterGreenApples(list);

        System.out.println(greenApples);
    }

}
